package com.android.controller_bt;

import com.wangbl.manager.listener.SimpleNotifyListener;
import com.wangbl.manager.utils.Logger;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ControllerActivity extends BaseActivity implements /*OnClickListener,*/ OnTouchListener {

	protected String TAG = "ControllerActivity";
	
	private int[] resId = { R.drawable.ic_a, R.drawable.ic_b, R.drawable.ic_c, R.drawable.ic_d, R.drawable.ic_e,
			R.drawable.ic_f, R.drawable.ic_g, R.drawable.ic_h, R.drawable.ic_xing, R.drawable.ic_i, R.drawable.ic_j,
			R.drawable.ic_jing };

	// m 对应 * n对应#
	private String[] cmds = { "a", "b", "c", "d", "e", "f", "g", "h", "m", "i", "j", "n" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_controller);
		ImageView left = (ImageView) findViewById(R.id.controller_left_btn);
		ImageView right = (ImageView) findViewById(R.id.controller_right_btn);
		ImageView up = (ImageView) findViewById(R.id.controller_up_btn);
		ImageView down = (ImageView) findViewById(R.id.controller_down_btn);
		
		GridView gridView = (GridView) findViewById(R.id.gridview);

		left.setOnTouchListener(this);
		right.setOnTouchListener(this);
		up.setOnTouchListener(this);
		down.setOnTouchListener(this);

//		left.setOnClickListener(this);
//		right.setOnClickListener(this);
//		up.setOnClickListener(this);
//		down.setOnClickListener(this);
		
		gridView.setAdapter(new MyAdapter());
	}
	
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return resId.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView view = new ImageView(ControllerActivity.this);
			view.setImageResource(resId[position]);
			view.setTag(position);
			view.setClickable(true);
			view.setOnTouchListener(touchListener);
			return view;
		}
	}

	private OnTouchListener touchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				String c = cmds[(Integer) v.getTag()];
				if ("i".equals(c) || "j".equals(c)) {
					manager.sendCmd(c);
				}
				break;
			case MotionEvent.ACTION_UP:
				Logger.i(TAG, "ACTION_UP");
				String c2 = cmds[(Integer) v.getTag()];
				if ("i".equals(c2) || "j".equals(c2)) {
					manager.interrupt();
				} else {
					manager.sendCmd(c2);
				}
				break;
			case MotionEvent.ACTION_CANCEL:
				Logger.i(TAG, "ACTION_CANCEL");
				String c3 = cmds[(Integer) v.getTag()];
				if ("i".equals(c3) || "j".equals(c3)) {
					manager.interrupt();
				} 
				break;
			}
			return false;
		}
	};

//	private void handleABCD(int id) {
//		switch (id) {
//		case R.id.controller_a_btn:
//			Logger.i(TAG, "a");
//			 manager.a();
//			break;
//		case R.id.controller_b_btn:
//			Logger.i(TAG, "b");
//			manager.b();
//			break;
//		case R.id.controller_c_btn:
//			Logger.i(TAG, "c");
//			manager.c();
//			break;
//		case R.id.controller_d_btn:
//			Logger.i(TAG, "d");
//			manager.d();
//			break;
//
//		}
//	}

//	@Override
//	public void onClick(View v) {
//		Logger.i(TAG, "onClick");
//		switch (v.getId()) {
//		case R.id.controller_left_btn:
//		case R.id.controller_right_btn:
//		case R.id.controller_up_btn:
//		case R.id.controller_down_btn:
//			manager.stop();
//			break;
//		}
//
//	}

	@Override
	protected void onStart() {
		super.onStart();
		manager.register(listener);

	}
	
	@Override
	protected void onStop() {
		super.onStop();
		manager.unregister(listener);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Logger.i(TAG, "ACTION_DOWN");
			handleDown(v.getId());
			break;
		case MotionEvent.ACTION_CANCEL:
			Logger.i(TAG, "ACTION_CANCEL");
			break;
		case MotionEvent.ACTION_UP:
			Logger.i(TAG, "ACTION_UP");
			manager.stop();
			break;
		}
		return false;
	}

	private void handleDown(int id) {
		switch (id) {
		case R.id.controller_left_btn:
			manager.left();
			break;
		case R.id.controller_right_btn:
			manager.right();
			break;
		case R.id.controller_up_btn:
			manager.up();
			break;
		case R.id.controller_down_btn:
			manager.down();
			break;

		}
	}

	SimpleNotifyListener listener = new SimpleNotifyListener() {

		@Override
		public void onConnectLose() {
			consumeFailureAlert("与设备断开连接");
		};
	};

	private void consumeFailureAlert(String str) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示!").setMessage(str).setPositiveButton("重新连接", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		}).setCancelable(false).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		manager.disconnect();
	}
}
